##########################################################
#####                                               ######
#####       Calculate parliament glove vectors      ######
#####                                               ######
##########################################################

trim <- function(s) gsub("^[[:space:]]+|[[:space:]]+$","",s)

# Load libraries

library(quanteda) # v3.3.1
library(quanteda.dictionaries) # [github::kbenoit/quanteda.dictionaries] v0.4
library(text2vec) # v0.6.3
set.seed(190795)

# Load data

load("working/debate_fcm.Rdata")

## Fit GLOVE model

glove = GlobalVectors$new(rank = 150, x_max = 2500L, learning_rate = .145)
debate_main = glove$fit_transform(debate_fcm, n_iter = 500, convergence_tol = 0.005, n_threads = 3)

debate_context = glove$components

word_vectors = debate_main + t(debate_context)

save(word_vectors, file = "working/word_vectors_150.Rdata")

